package com.xydtech.ecard.config.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.config.domain.EcardGridManage;
import com.xydtech.ecard.config.service.IEcardGridManageService;
import com.xydtech.system.domain.FivelevelAddr;
import com.xydtech.system.service.IFivelevelAddrService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 网格配置Controller
 *
 * @author JHON
 * @date 2022-08-04
 */
@Controller
@RequestMapping("/config/gridManage")
public class EcardGridManageController extends BaseController {
    private String prefix = "config/gridManage";

    @Autowired
    private IEcardGridManageService ecardGridManageService;
    @Autowired
    private IFivelevelAddrService fiveLevelAddrService;

    @RequiresPermissions("config:gridManage:view")
    @GetMapping()
    public String gridManage(ModelMap mmap) {
        mmap.put("gridCode", ShiroUtils.getSysUser().getDept().getGridCode());
        return prefix + "/gridManage";
    }

    /**
     * 查询网格配置列表
     */
    @RequiresPermissions("config:gridManage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardGridManage ecardGridManage) {
        if (ecardGridManage.getDeptId() == null) {
            ecardGridManage.setDeptId(ShiroUtils.getSysUser().getDeptId());
        }
        startPage();
        List<EcardGridManage> list = ecardGridManageService.selectEcardGridManageList(ecardGridManage);
        return getDataTable(list);
    }

    /**
     * 导出网格配置列表
     */
    @RequiresPermissions("config:gridManage:export")
    @Log(title = "网格配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardGridManage ecardGridManage) {
        List<EcardGridManage> list = ecardGridManageService.selectEcardGridManageList(ecardGridManage);
        ExcelUtil<EcardGridManage> util = new ExcelUtil<EcardGridManage>(EcardGridManage.class);
        return util.exportExcel(list, "网格配置数据");
    }

    /**
     * 新增网格配置
     */
    @GetMapping("/add/{gridCode}")
    public String add(@PathVariable("gridCode") String gridCode, ModelMap mmap) {
        FivelevelAddr fivelevelAddr = fiveLevelAddrService.selectSysFivelevelAddrById(gridCode);
        mmap.put("gridName", fivelevelAddr.getName());
        mmap.put("gridCode", gridCode);
        return prefix + "/add";
    }

    /**
     * 新增保存网格配置
     */
    @RequiresPermissions("config:gridManage:add")
    @Log(title = "网格配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardGridManage ecardGridManage) {
        return toAjax(ecardGridManageService.insertEcardGridManage(ecardGridManage));
    }

    /**
     * 修改网格配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        EcardGridManage ecardGridManage = ecardGridManageService.selectEcardGridManageById(id);
        mmap.put("ecardGridManage", ecardGridManage);
        return prefix + "/edit";
    }

    /**
     * 选择人员
     */
    @GetMapping("/choiceUser")
    public String choiceUser() {
        return prefix + "/choiceUser";
    }

    /**
     * 修改保存网格配置
     */
    @RequiresPermissions("config:gridManage:edit")
    @Log(title = "网格配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardGridManage ecardGridManage) {
        return toAjax(ecardGridManageService.updateEcardGridManage(ecardGridManage));
    }

    /**
     * 删除网格配置
     */
    @RequiresPermissions("config:gridManage:remove")
    @Log(title = "网格配置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ecardGridManageService.deleteEcardGridManageByIds(ids));
    }
}
